package com.example.mongodb.mongodb.controller.rabbitmq.direct;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ReceiveWarnLogDirect {

    public static void main(String[] args) {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("123.207.179.78");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");


        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            //申明交换机
            channel.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
            //申明队列
            String queue = channel.queueDeclare().getQueue();
            System.out.println("队列名称："+queue);
            //String queue, String exchange, String routingKey, Map<String, Object> arguments
            channel.queueBind(queue,"direct_logs","warn",null);

            DeliverCallback callback = (consumerTat,deliver)->{
                String message = new String(deliver.getBody(), "UTF-8");
                System.out.println("[Received] Exchange: "+deliver.getEnvelope().getExchange()+" [ RoutingKey :] "+deliver.getEnvelope().getRoutingKey()+"    "+message);
            };

            channel.basicConsume(queue,callback,consumerTag->{
                System.out.println("消费者Tag===========>>>>>"+consumerTag);
            });

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }


    }
}
